#include "badminton.h"
#include "qgraphicsscene.h"
#include <QGraphicsScene>
#include <QDebug>
#include <QGraphicsPixmapItem>
#include <QGraphicsItemGroup>
#include "racket.h"

Badminton::Badminton(QObject *parent1,QGraphicsItem *parent)
    : QObject(parent1),QGraphicsPixmapItem(parent) , Vx(0), Vy(0),Gravity(0.3)
{
    //加载羽毛球照片
    this->setPixmap(QPixmap(":/lng/picture/OIP-C.jpg"));
    GameTimer = new QTimer(this);
    QInputDevice::connect(GameTimer, &QTimer::timeout, this, &Badminton::move);
    GameTimer->start(10);
}
Badminton::~Badminton()
{
    delete GameTimer;
}
void Badminton::setPlayers(Player1* p1, Player2* p2)//设置玩家
{
    player1 = p1;
    player2 = p2;
}

void Badminton::setAirWall(QGraphicsRectItem *Wall1,QGraphicsRectItem *Wall2,QGraphicsRectItem *Wall3)//设置空气墙
{
   airWall1 = Wall1;
   airWall2 = Wall2;
   airWall3 = Wall3;
}

bool Badminton::checkAirWallCollision(QGraphicsRectItem *airWall)
{
    //if(!airWall) return false;

    // 获取羽毛球和球拍的边界矩形
    QRectF badmintonRect = this->boundingRect();
    badmintonRect.moveTo(this->pos());

    QRectF wallRect = airWall->boundingRect();
    wallRect.moveTo(airWall->scenePos());

    // 检测碰撞
    return badmintonRect.intersects(wallRect);
}


bool Badminton::checkRacketCollision(Racket* racket)
{
    if(!racket || !racket->m_Net) return false;
    
    // 获取羽毛球和球拍的边界矩形
    QRectF badmintonRect = this->boundingRect();
    badmintonRect.moveTo(this->pos());
    
    QRectF racketRect = racket->m_Net->boundingRect();
    racketRect.moveTo(racket->m_Net->scenePos());
    
    // 检测碰撞
    return badmintonRect.intersects(racketRect);
}

void Badminton::move() 
{
    // 空气阻力系数 (0-1之间，值越小阻力越大)
    const double airResistance = 0.99;
    
    // 应用空气阻力 - 逐渐减小速度
    Vx *= airResistance;
    Vy *= airResistance;
    
    // 最小速度阈值，避免速度无限接近0
    const double minSpeed = 0.03;
    if(fabs(Vx) < minSpeed) Vx = Vx/airResistance;
    
    // 应用重力
    Vy += Gravity;
    
    // 移动羽毛球
    setPos(x() + Vx, y() + Vy);
    
    // 检测是否落地 (假设地面Y坐标为600)
    if(this->scenePos().y() >= 780)
    {
        emit landed();  // 发射落地信号
    }
    
    // 检测与球拍碰撞
    if(player1->m_MoveUpFlag&&
        player1->m_MoveRollFlag&&player1->Played&&player1 &&
        player1->m_Racket && checkRacketCollision(player1->m_Racket)) {
        // 与玩家1球拍碰撞后的反弹逻辑
        player1->Played = false;//player2击球后才可击球
        player2->Played = true;
        Vx = 22;  // 向右反弹
        Vy = 0.01;        // 向下反弹
    }
    if(player2->m_MoveUpFlag&&
        player2->m_MoveRollFlag&&player2->Played&&player2 &&
        player2->m_Racket && checkRacketCollision(player2->m_Racket)) {
        // 与玩家1球拍碰撞后的反弹逻辑
        player2->Played = false;//player2击球后才可击球
        player1->Played = true;
        Vx = -22;  // 向右反弹
        Vy = 0.01;        // 向下反弹
    }
    if(player1->m_MoveRollFlag&&player1->Played&&player1 && player1->m_Racket && checkRacketCollision(player1->m_Racket)) {
        // 与玩家1球拍碰撞后的反弹逻辑
        player1->Played = false;//player2击球后才可击球
        player2->Played = true;
        Vx = 12;  // 向右反弹
        Vy = -14;        // 向上反弹
    }
    
    if(player2->m_MoveRollFlag&&player2->Played && player2 && player2->m_Racket && checkRacketCollision(player2->m_Racket)) {
        // 与玩家2球拍碰撞后的反弹逻辑
        player2->Played = false;//player2击球后才可击球
        player1->Played = true;
        Vx = -12;   // 向左反弹
        Vy = -14;        // 向上反弹
    }
    if(checkAirWallCollision(airWall1)||checkAirWallCollision(airWall2)||checkAirWallCollision(airWall3))
    {
        Vx = -0.8*Vx;   // 向反方向反弹
        Vy = 0.9*Vy;
    }
    // 检测与空气墙碰撞
    if( airWall1&& collidesWithItem(airWall1))
    {
        // 左墙碰撞 - 向右反弹
        Vx = abs(Vx);
    }
    
    if(airWall2 && collidesWithItem(airWall2))
    {
        // 球网墙碰撞 - 根据位置决定反弹方向
        Vx = -Vx;
    }
    
    if(airWall3 && collidesWithItem(airWall3))
    {
        // 右墙碰撞 - 向左反弹
        Vx = -abs(Vx);
    }
}
